//
//  UIViewController+Message.m
//  测试
//
//  Created by 李李善 on 2017/3/18.
//  Copyright © 2017年 森之源. All rights reserved.
//

#import "UIViewController+Message.h"
#import "UIView+Extension.h"
#import <MBProgressHUD/MBProgressHUD.h>
@implementation UIViewController (Message)


 - (void)showMessage:(NSString *)msg
{
  
    if ([NSThread isMainThread]) {
        [self showText:msg];
    }else
    {
          __weak typeof(self) weakSelf = self;
        dispatch_async(dispatch_get_main_queue(),^{
         [weakSelf showText:msg];
        });
    }
    
}

-(void)showText:(NSString *)text
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.userInteractionEnabled = YES;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    if ([text isKindOfClass:[NSString class]]) {
        hud.label.text = text;
    }
    else if ([text isKindOfClass:[NSError class]]) {
        hud.label.text  = [(NSError *)text localizedDescription];
    }
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:0.5f];
    
}


- (void)showBarMessage:(NSString *)msg{
    __block UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 24, self.view.width, 40)];
    btn.enabled = NO;
    btn.titleLabel.font=[UIFont boldSystemFontOfSize:15];
    [btn setBackgroundColor:[UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.6]];
    btn.alpha=0.2;
    [btn setTitle:msg forState:UIControlStateNormal];
    //    [btn setImage:[UIImage imageNamed:@"cold_loading_selected_new"] forState:UIControlStateNormal];
    
    [self.navigationController.view insertSubview:btn aboveSubview:self.navigationController.navigationBar];
    
    [UIView animateWithDuration:0.15f animations:^{
        btn.alpha=1;
        btn.transform=CGAffineTransformMakeTranslation(0, 40);
    } completion:^(BOOL finished) {
        //停止一秒 在执行动画
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.15f animations:^{
                btn.transform=CGAffineTransformIdentity;
                btn.alpha=0.2;
            } completion:^(BOOL finished) {
                [btn removeFromSuperview];
            }];
        });
        
        
        
    }];
}

@end


